﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Schedule.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using IveelySE.Common;

#endregion

namespace IveelySE.Run.Task
{
    /// <summary>
    ///   执行计划类
    /// </summary>
    internal class Schedule
    {
        /// <summary>
        ///   构造方法
        /// </summary>
        /// <param name="name"> 时间计划名称 </param>
        public Schedule()
        {
            this.Name = Guid.NewGuid().ToString();
            this.Status = State.Queue;
        }

        /// <summary>
        ///   计划任务名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        ///   上次执行时间
        /// </summary>
        public DateTime LastSubmit { get; set; }

        /// <summary>
        ///   执行间隔时间（以分钟为单位）
        /// </summary>
        public double Minute { get; set; }

        /// <summary>
        ///   最长运行时间
        /// </summary>
        public double MaxRunning { get; set; }

        /// <summary>
        ///   需要执行的计划任务
        /// </summary>
        public ITask Task { get; set; }

        /// <summary>
        ///   状态
        /// </summary>
        public State Status { get; set; }

        /// <summary>
        ///   启动计划任务
        /// </summary>
        /// <typeparam name="T"> 被执行的Task </typeparam>
        public void Start()
        {
            this.Start(null);
        }

        /// <summary>
        ///   启动计划任务
        /// </summary>
        /// <typeparam name="T"> 被执行的Task </typeparam>
        /// <param name="parameters"> 执行Task需要的参数 </param>
        public void Start(object parameters)
        {
            this.Task.Start(parameters);
        }

        /// <summary>
        ///   停止计划
        /// </summary>
        public void Stop()
        {
            Task.Stop();
        }

        #region Nested type: State

        /// <summary>
        ///   执行状态
        /// </summary>
        internal enum State
        {
            /// <summary>
            ///   正在排队
            /// </summary>
            Queue = 0,

            /// <summary>
            ///   正在执行
            /// </summary>
            Running = 1,

            /// <summary>
            ///   已经完成
            /// </summary>
            Compelete = 2
        }

        #endregion
    }
}